/**
 * Copyright 2013 Net355 Co,.Ltd. 
 * All rights reserved.
 *
 * "ConfServlet.java is for what: "
 * 
 * Create by Ray at 2013-12-6 下午2:17:17.
 * Ray [email:rayinhangzhou@126.com]
 * 
 * Revision History
 *
 * Date            Programmer                   Notes
 * ---------    ---------------------  -----------------------------------
 * 2013-12-6            Ray                       initial
 **/

package com.net355.eblog.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.net355.eblog.util.CacheHelper;
import com.net355.eblog.util.GlobalParams;
import com.net355.eblog.util.SyncHelper;

public class ConfServlet extends HttpServlet {
	/**
	 * serial id
	 */
	private static final long serialVersionUID = -2819896512667530335L;
	private static Log LOG = LogFactory.getLog(ConfServlet.class);

	/**
	 * Constructor of the object.
	 */
	public ConfServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
		CacheHelper.set_cache(GlobalParams.CACHE_NAME_COUNT_CHAT, GlobalParams.COUNT_CHAT.get());
		CacheHelper.set_cache(GlobalParams.CACHE_NAME_COUNT_SEARCH, GlobalParams.COUNT_SEARCH.get());
		CacheHelper.set_cache(GlobalParams.CACHE_NAME_COUNT_UPDATE, GlobalParams.COUNT_UPDATE.get());
		CacheHelper.set_cache(GlobalParams.CACHE_NAME_COUNT_VIEW, GlobalParams.COUNT_VIEW.get());
		System.exit(0);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		System.setProperty("file.encoding", "UTF-8");
		try {
			Properties properties = new Properties();
			ServletContext context = this.getServletContext();
			InputStream in = context.getResourceAsStream("/WEB-INF/classes/conf.properties");
			properties.load(in);
			GlobalParams.DB_DATA_URL = properties.getProperty("data.db.url");
			GlobalParams.BLOG_OWNER = properties.getProperty("auth.owner");
			String[] notebooks_key = properties.getProperty("auth.books.key").split(",");
			String[] notebooks_name = properties.getProperty("auth.books.name").split(",");
			for (int i = 0; i < notebooks_key.length; i++) {
				GlobalParams.NOTEBOOK_FILTER.put(notebooks_key[i], notebooks_name[i]);
			}
			in.close();
			
			//初始化evernote同步线程
			SyncHelper.initSyncWithEvernote();

			// 初始化缓存文件
			LOG.info("webPath-->" + context.getRealPath("WEB-INF"));
			System.setProperty("ehcache.disk.store.dir", context.getRealPath("WEB-INF") + "/cache/");
			System.setProperty("net.sf.ehcache.enableShutdownHook", "true");
			CacheHelper.initEhCache(context.getResourceAsStream("/WEB-INF/classes/ehcache.xml"));
			GlobalParams.COUNT_CHAT = new AtomicInteger((Integer)CacheHelper.get_cache(GlobalParams.CACHE_NAME_COUNT_CHAT));
			GlobalParams.COUNT_SEARCH = new AtomicInteger((Integer) CacheHelper.get_cache(GlobalParams.CACHE_NAME_COUNT_SEARCH));
			GlobalParams.COUNT_UPDATE = new AtomicInteger((Integer) CacheHelper.get_cache(GlobalParams.CACHE_NAME_COUNT_UPDATE));
			GlobalParams.COUNT_VIEW = new AtomicInteger((Integer) CacheHelper.get_cache(GlobalParams.CACHE_NAME_COUNT_VIEW));
			LOG.info("ray_print-COUNT_CHAT-->" + GlobalParams.COUNT_CHAT.get());
			LOG.info("ray_print-COUNT_SEARCH-->" + GlobalParams.COUNT_SEARCH.get());
			LOG.info("ray_print-COUNT_UPDATE-->" + GlobalParams.COUNT_UPDATE.get());
			LOG.info("ray_print-COUNT_VIEW-->" + GlobalParams.COUNT_VIEW.get());
			
			LOG.info("WEB SYSTEM STARTED -------------------->");
			
			
		} catch (Exception e) {
			LOG.error("START WEB ERROR--->>>", e);
		}
	}
}
